import 'package:flutter/material.dart';

import '../services/ScreenAdaper.dart';
import '../utils/ToastMsg.dart';

class CartNum extends StatefulWidget {
  int num = 1;

  //最大值
  int max = 0;
  int countNum = 1;

  //最小值
  int min = 1;
  double width = 0;

  //高度
  double height = 0;
  final Function onChangedRemove;
  final Function onChangedAdd;

  CartNum(
      {this.num = 1,
      required this.max,
      this.min = 1,
      required this.countNum,
      required this.onChangedRemove,
      required this.onChangedAdd,
      this.width = 0,
      this.height = 0,
      Key? key})
      : super(key: key);

  @override
  _CartNumState createState() => _CartNumState();
}

class _CartNumState extends State<CartNum> {
  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Container(
      height: this.widget.height > 0 ? this.widget.height : ScreenAdapter.height(25),
      width: this.widget.width > 0 ? this.widget.width : ScreenAdapter.width(80),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          InkWell(
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.grey,
                  borderRadius:
                      BorderRadius.only(topLeft: Radius.circular(ScreenAdapter.width(5)), bottomLeft: Radius.circular(ScreenAdapter.width(5)))),
              width: ScreenAdapter.width(17),
              height: ScreenAdapter.height(17),
              alignment: Alignment.center,
              child: Text(
                "-",
                style: TextStyle(
                  color: Colors.black,
                  fontSize: ScreenAdapter.fontSize(10),
                ),
                textAlign: TextAlign.center,
              ),
            ),
            onTap: () {
              if (this.widget.countNum > this.widget.min) {
                this.widget.countNum = this.widget.countNum - 1;
                setState(() {});
              } else {
                ToastMsg.show("不能再减了");
              }
            },
          ),
          Container(
            alignment: Alignment.center,
            height: ScreenAdapter.height(17),
            width: ScreenAdapter.width(30),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.grey),
            ),
            child: Text(
              "${this.widget.countNum}",
              style: TextStyle(
                color: Colors.black,
                fontSize: ScreenAdapter.fontSize(10),
              ),
            ),
          ),
          InkWell(
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.grey,
                  borderRadius:
                      BorderRadius.only(topRight: Radius.circular(ScreenAdapter.width(5)), bottomRight: Radius.circular(ScreenAdapter.width(5)))),
              width: ScreenAdapter.width(17),
              height: ScreenAdapter.height(17),
              alignment: Alignment.center,
              child: Text(
                "+",
                style: TextStyle(
                  color: Colors.black,
                  fontSize: ScreenAdapter.fontSize(10),
                ),
              ),
            ),
            onTap: () {
              if (this.widget.countNum <= this.widget.max) {
                this.widget.countNum = this.widget.countNum + 1;
                setState(() {});
              } else {
                ToastMsg.show("不能再加了");
              }
            },
          ),
        ],
      ),
    );
  }
}
